package com.sun.common;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.sun.entity.Goods;
import com.sun.mapper.GoodsMapper;

import java.util.ArrayList;
import java.util.List;

public class UploadDataListener extends AnalysisEventListener<Goods> {

    private List<Goods> goodsList = new ArrayList<>();
    private GoodsMapper goodsMapper;

    public UploadDataListener(GoodsMapper goodsMapper) {
        this.goodsMapper = goodsMapper;
    }

    /**
     * 每一条数据解析都会来调用
     */
    @Override
    public void invoke(Goods goods, AnalysisContext analysisContext) {
        goodsList.add(goods);
    }

    /**
     * 所有数据解析完成了，会来调用
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 数据存储到数据库，防止内存溢出
//        saveData();
        System.out.println("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
//    private void saveData() {
//        goodsMapper.insertBatch(goodsList);
//    }
    public List<Goods> getGoodsList() {
        return goodsList;
    }
}
